package com.xbongbong.crm.perform.query.list.button;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.message.model.PushModel;
import com.xbongbong.saas.enums.ListOptionEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.elasticsearch.common.inject.Singleton;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 提醒
 * @author 吴峰
 * @date 2021/12/20 14:47
 */
@Component
public class Remind {

    @Resource
    private PushModel pushModel;

    private boolean enable;

    private Collection<Long> list;


    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public Collection<Long> getList() {
        return list;
    }

    public void setList(Collection<Long> list) {
        this.list = list;
    }


    public Remind(boolean enable, Collection<Long> list) {
        this.enable = enable;
        this.list = list;
    }

    public Remind() {
        enable = false;
        list = new ArrayList<>();
    }


    private Remind getRemind(Collection<Long> dataIdIn, String corpid, Integer businessType, Integer subBusinessType) {
        List<Integer> notRemindList = Arrays.asList(XbbRefTypeEnum.CUSTOMER_RECYCLE.getCode(), XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode(), XbbRefTypeEnum.CLUE_PUBLIC.getCode(), XbbRefTypeEnum.CLUE_RECYCLE.getCode());
        if (!notRemindList.contains(subBusinessType)) {
            Collection<Long> list = pushModel.getPushNotify(dataIdIn, corpid, businessType);
            return new Remind(true, list);
        } else {
            return new Remind(false, new ArrayList<>());
        }
    }

    public Remind setRemindField( String corpid, Integer businessType, Integer subBusinessType, List<Long> dataIdList, List<FieldAttrEntity> list) throws XbbException {
        Remind remind = getRemind(dataIdList, corpid, businessType, subBusinessType);
        if (remind.isEnable()) {
            FieldAttrEntity fieldRemind = new FieldAttrEntity();
            fieldRemind.setAttr(ListOptionEnum.REMIND.getAttr());
            fieldRemind.setAttrName(ListOptionEnum.REMIND.getAttrName());
            fieldRemind.setFieldType(FieldTypeEnum.REMIND.getType());
            fieldRemind.setDisableListSort(1);
            list.add(0, fieldRemind);
        }
        return remind;
    }

    public static void setRemindData(Remind remind, PaasFormDataEntityExt paasFormDataEntityExt) {
        if (remind.isEnable()) {
            JSONObject json = new JSONObject();
            Long dataId = paasFormDataEntityExt.getId();
            JSONObject data = paasFormDataEntityExt.getData();
            json.put("value", remind.getList().contains(dataId) ? 1 : 0);
            json.put("businessType", XbbRefTypeEnum.PUSH_NOTIFY.getCode());
            data.put(ListOptionEnum.REMIND.getAttr(), json);
        }

    }

}
